package com.hr;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.espirit.eap.manager.DepartmentService;
import com.espirit.eap.sa.Employee;
import com.googlecode.cswish.annotation.ParamScope;
import com.googlecode.cswish.model.PageElements;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.model.ParameterMap;
import com.googlecode.cswish.model.Reference;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.struts.spring.ObjectFactory;
import com.googlecode.cswish.struts.spring.SearchCallback;

@Service
public class DeliveryTimeItemService {
	@Resource
	GenericService genericService;
	
	@Resource
	DepartmentService departmentService;
	
//	@Resource
	SalaryService salaryService;
	
	public  PageInfo search(final ParameterMap model, int start, int limit,
    		String sort, String dir, boolean newSearch, Reference<QLInfo> qlInfoRef,
    		@ParamScope(visible=false, value="#ps")PageElements ps ,boolean fromMenu
    		) {
		
		SearchCallback callback = departmentService.buildDeptDefaultSearchCallback(model, null, null);
		return genericService.search(model, start, limit, sort, dir, newSearch, qlInfoRef, callback, true, false);
	}
	
	@Transactional
	public void add(DeliveryTimeItem model){
		model = genericService.add(model);
		
		getSalaryService().reCalEmpSalary(model.getEmployee());
	}
	
	@Transactional
	public void update(DeliveryTimeItem model){
		model = genericService.update(model);
		
		getSalaryService().reCalEmpSalary(model.getEmployee());
	}
	
	@Transactional
	public void deleteList(List<DeliveryTimeItem> results){
		if (results == null) return ;
		for(DeliveryTimeItem deliveryTimeItem : results){
			if (deliveryTimeItem == null || deliveryTimeItem.getId() == null) continue;
			deliveryTimeItem = genericService.load(DeliveryTimeItem.class, deliveryTimeItem.getId());
			Employee emp = deliveryTimeItem.getEmployee();
			genericService.delete(deliveryTimeItem);
			
			getSalaryService().reCalEmpSalary(emp);
		}
		
	}
	
	public SalaryService getSalaryService(){
		if (salaryService == null){
			salaryService = ObjectFactory.getInstance().getBean(SalaryService.class, true);
		}
		return salaryService;
	}
}
